package com.jiyun.controller;

import com.github.pagehelper.PageInfo;
import com.jiyun.feign.FeignTitle;
import com.jiyun.model.day14.Member;
import com.jiyun.service.MemberService;
import com.jiyun.vo.Result;
import com.jiyun.vo.day07.CatalogDto;
import com.jiyun.vo.day08.CourseCto;
import com.jiyun.vo.day08.CourseVo;
import com.jiyun.vo.day11.RollVo;
import com.jiyun.vo.day12.RivalDto2;
import com.jiyun.vo.day12.RivalVo;
import com.jiyun.vo.day15.TraineeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Author mtt
 * @Date 2023/8/22 11：02
 **/
@RestController
@RequestMapping("members")
public class MemberController {

    @Autowired
    private MemberService memberService;

    @RequestMapping("login")
    public Result login(Member member){
        try {
            String token = memberService.login(member);
            if(token!=null){
                return Result.success(token);
            }
            return Result.error();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }
    //展示页面竞赛数量
    @RequestMapping("findByIdRival")
    public Result findByIdRival(HttpServletRequest request){
        try {
            int a = memberService.findByIdRival(request);
            return Result.success(a);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }
    @RequestMapping("findByIdCourse")
    public Result findByIdCourse(HttpServletRequest request){
        try {
            int a = memberService.findByIdCourse(request);
            return Result.success(a);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }
    @RequestMapping("findByIdTrainee")
    public Result findByIdTrainee(HttpServletRequest request){
        try {
            int a = memberService.findByIdTrainee(request);
            return Result.success(a);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }
    @RequestMapping("findAll")
    public Result findAll(){
        try {
            List<CourseVo> list = memberService.findAll();
            return Result.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }
    //课程点赞
    @RequestMapping("updateDianZan/{id}")
    public Result updateDianZan(@PathVariable("id") Integer id){
        try {
            memberService.updateDianZan(id);
            return Result.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }
    //竞赛点赞
    @RequestMapping("dinaZanRoll/{id}")
    public Result dinaZanRoll(@PathVariable("id") Integer id){
        try {
            memberService.dinaZanRoll(id);
            return Result.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }
    //竞赛取消点赞
    @RequestMapping("qvXiaoRoll/{id}")
    public Result qvXiaoRoll(@PathVariable("id") Integer id){
        try {
            memberService.qvXiaoRoll(id);
            return Result.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }

    }
    @RequestMapping("moRen")
    public Result moRen(){
        try {
            memberService.moRen();
            return Result.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }
    //最新课程
    @RequestMapping("findReCourse")
    public Result findReCourse(){
        try {
            List<CourseVo> list = memberService.findReCourse();
            return Result.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }
    //最新竞技
    @RequestMapping("findReRoll")
    public Result findReRoll(){
        try {
            List<RollVo> list = memberService.findReRoll();
            return Result.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }
    @RequestMapping("findReTrainee")
    public Result findReTrainee(){
        try {
            List<TraineeVo> list = memberService.findReTrainee();
            return Result.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }

    }
    //查询更多课程详情  分页条件查询
    @RequestMapping("findDetailAll")
    public Result findDetailAll(@RequestBody CourseCto courseCto){
        try {
            PageInfo<CourseVo> page =  memberService.findDetailAll(courseCto);
            return Result.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }
    @Autowired
    private FeignTitle feignTitle;
    //查询所有标签信息
    @RequestMapping("findTitle")
    public Result findTitle(){
        try {
            Result all2 = feignTitle.findAll2();
            return all2;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }
    //查看单个课程详情
    @RequestMapping("findDetailCourseOne/{id}")
    public Result findDetailCourseOne(@PathVariable("id") Integer id,HttpServletRequest request){
        try {
            Map<String,Object> map = memberService.findDetailCourseOne(id,request);
            return Result.success(map);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }
    //完成课时状态变更
    @RequestMapping("updateCatalogStatus")
    public Result updateCatalogStatus(@RequestBody CatalogDto dto,HttpServletRequest request){
        try {
            memberService.updateCatalogStatus(dto,request);
            return Result.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }
    //查看之前先拷贝
    @RequestMapping("addCataloges/{id}")
    public Result addCataloges(@PathVariable("id")String id,HttpServletRequest request){
        try {
            memberService.addCataloges(id,request);
            return Result.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }
    //C端展示竞赛
    @RequestMapping("findRivalAll")
    public Result findRivalAll(@RequestBody RivalDto2 dto){
        try {
            PageInfo<RivalVo> a = memberService.findRivalAll(dto);
            return Result.success(a);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    //C端展示单个成员与竞赛的详情
    @RequestMapping("findRivalDetail/{id}")
    public Result findRivalDetail(@PathVariable("id") String id,HttpServletRequest request){
        try {
            RivalVo rivalVo = memberService.findRivalDetail(id,request);
            return Result.success(rivalVo);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }


    //查询出当前登录人
    @RequestMapping("findByIdMember")
    public Result findByIdMember(HttpServletRequest request){
        try {
            return memberService.findByIdMember(request);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }
    //查询所有战队
    @RequestMapping("findAllTeam")
    public Result findAllTeam(){
        try {
            return memberService.findAllTeam();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }
    //报名传递过来一个数据往中间表存入数据
    @RequestMapping("addTeamMember/{id}")
    public Result addTeamMember(@PathVariable("id") String id,HttpServletRequest request){
        try {
            memberService.addTeamMember(id,request);
            return Result.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @RequestMapping("addRivalMember/{id}")
    public Result addRivalMember(@PathVariable("id")String id,HttpServletRequest request){
        try {
            memberService.addRivalMember(id,request);
            return Result.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

}
